package com.phoenix.filemanager;
import java.io.File;
import java.util.ArrayList;
import com.phoenix.Ulties.FileManagerConstants;
import com.phoenix.Ulties.FileManagerUlties;

import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class FileManagerMainActivity extends Activity {
	// arraylist parameter
	private ArrayList<String> list_file_sdcard;
	//listview parameter
	private GridView grid_view;

	private String path;

	private AlertDialog.Builder alertlistDialog;

	DialogInterface dialog;

	private FileManageBaseAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_file_manager_main);
		if ((GridView)findViewById(R.id.grid_view)!=null) {
			grid_view = (GridView)findViewById(R.id.grid_view);
		}
		//		check sdcard is ready
		if (FileManagerUlties.isStorageAvailable()==true) {
			list_file_sdcard = new ArrayList<String>();

			//	begin get list file sdcard;		
			list_file_sdcard = FileManagerUlties.getListFileSDcard();
			if (list_file_sdcard.size()>0) {
				//				sort by name

				list_file_sdcard= FileManagerUlties.sortByName(list_file_sdcard);

				adapter = new  FileManageBaseAdapter(getBaseContext(),FileManagerConstants.RootLinksSDCard,list_file_sdcard);
				grid_view.setAdapter(adapter);
				//   set onitemclick
				grid_view.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						path="";
						String name_file = list_file_sdcard.get(arg2);
						path = FileManagerConstants.RootLinksSDCard+"/"+name_file;
						Boolean is_folder = FileManagerUlties.isDirectory(path);
						if (is_folder==true) {
							//							String name_file = list_file_sdcard.get(arg2);
							//							String path = FileManagerConstants.RootLinksSDCard+"/"+name_file;

							Intent intent = new Intent(FileManagerMainActivity.this,FileManagerDetailActivity.class);
							intent.putExtra(FileManagerConstants.PathLink, path);
							startActivity(intent);

						}
						else 
						{
							if (FileManagerUlties.isImageFile(list_file_sdcard.get(arg2))==true) {
//View Image
								Intent intent = new Intent();
								intent.setAction(Intent.ACTION_VIEW);
								intent.setDataAndType(Uri.parse("file://" + path), "image/*");
								startActivity(intent);

							}
							else if (FileManagerUlties.isInstallFile(list_file_sdcard.get(arg2))==true) {
								Intent intent =new Intent();
								intent.setAction(Intent.ACTION_VIEW);
								intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
								startActivity(intent);
							}
						}
					}
				});
				//				set long press onitem
				grid_view.setOnItemLongClickListener(new OnItemLongClickListener() {
					@Override
					public boolean onItemLongClick(AdapterView<?> arg0,
							View arg1, final int arg2, long arg3) {
						// TODO Auto-generated method stub
						path="";
						String name_file = list_file_sdcard.get(arg2);
						path = FileManagerConstants.RootLinksSDCard+"/"+name_file;

						alertlistDialog=new AlertDialog.Builder(FileManagerMainActivity.this);
						alertlistDialog.setTitle(FileManagerConstants.MobileSystem).setItems(FileManagerConstants.items, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
								actionTouch(arg2,which);
							}
						});
						alertlistDialog.show();
						return false;
					}
				});
			}
		}
	}
	private void actionTouch(final int position,int which) {

		switch (which) {
		case 0:
		{
			//			Copy action
			copyFileSDcard(list_file_sdcard.get(position));
			break;
		}
		case 1:
		{
			if (FileManagerUlties.isDirectory(path)==true) {
				FileManagerUlties.deleteFolder(path);				
			}
			else
			{
				FileManagerUlties.deleteFile(path);
			}
			startActivity(new Intent(FileManagerMainActivity.this,FileManagerMainActivity.class));
			finish();
			break;
		}
		case 2:
		{
			//			Rename action
			AlertDialog.Builder editalert = new AlertDialog.Builder(this);
			editalert.setTitle("Rename File");
			final EditText input = new EditText(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT);
			input.setLayoutParams(lp);
			editalert.setView(input);
			input.setText(list_file_sdcard.get(position));
			editalert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

					String tmp_file_name = input.getText().toString();
					if (!tmp_file_name.equals("")) {
						String old_name = FileManagerConstants.RootLinksSDCard +  "/"+list_file_sdcard.get(position);
						String new_name=  FileManagerConstants.RootLinksSDCard + "/"+tmp_file_name;
						FileManagerUlties.renameFileSDCard(old_name, new_name);
						startActivity(new Intent(FileManagerMainActivity.this,FileManagerMainActivity.class));
						finish();
					}
				}
			});
			editalert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

				}
			});

			editalert.show();

			break;
		}
		default:
			break;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_file_manager_main, menu);
		return true;
	}	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_settings:
	        		
	        	SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
	        	String path_file_copy = preferences.getString(FileManagerConstants.KeyCopy,"name");
	        	String name_file_copy = preferences.getString(FileManagerConstants.KeyName,"name");
	        	if (!name_file_copy.equals("name")) {
	        		
	        		File dest  = new File(FileManagerConstants.RootLinksSDCard+"/"+name_file_copy);
	        		File scr = new File(path_file_copy);
	        		FileManagerUlties.copyFile(scr, dest);
	        	}
				Intent intent = new Intent(FileManagerMainActivity.this,FileManagerMainActivity.class);
				startActivity(intent);									
				finish();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	public void copyFileSDcard(String name_file)
	{
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString(FileManagerConstants.KeyCopy, path);
		editor.putString(FileManagerConstants.KeyName, name_file);
		editor.commit();
	}

}
